package com.fled.itsmbigscreen.service.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fled.itsmbigscreen.mapper.HuorongSysLogMapper;
import com.fled.itsmbigscreen.pojo.HuorongSysLog;
import com.fled.itsmbigscreen.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.FileReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class TaskImpl implements TaskService {

    @Autowired
    private HuorongSysLogMapper sysLogMapper;

    /**
     * 每小时同步火绒日志
     */
//    @Scheduled(cron = "0 0 */1 * * ?")
    @Scheduled(cron = "*/1")
    public void sysLog() {
        //获取上次读取的最大行数
        Integer i = sysLogMapper.maxLine();
        if (i==null){
            i=1;
        }
        int startLine = i;
//        String filePath = "/var/log/remote/Ecadi-ESCenter1/HRESS.log";
        String filePath = "C:\\Users\\Eric\\Desktop\\HRESS.log";
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
             // 从第10行开始读取
            int currentLine = 1;
            while ((line = br.readLine()) != null) {
                if (currentLine >= startLine) {
                    //                long lineCount = br.lines().();
//                System.out.println(lineCount+":"+line)
//                System.out.println(line);
                    //时间
                    String[] parts = line.split(" ");
                    String time = parts[0];
                    if (!time.contains(".")) {
                        time = time.replace("+", ".00+");
                    }
                    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
                    Date date = df.parse(time);
                    System.out.println(date);
//                if ()
                    //获取json
                    String[] partsJson = line.split("]: ");
                    String resultJson = partsJson[1].trim();
                    System.out.println(resultJson);
                    currentLine++;
                    System.out.println(currentLine);
                    HuorongSysLog huorongSysLog = new HuorongSysLog();
                    try {
                        ObjectMapper objectMapper = new ObjectMapper();
                        JsonNode jsonNode = objectMapper.readTree(resultJson);
                        if (jsonNode.get("event_type") != null) {
                            String eventType = jsonNode.get("event_type").asText();
                            String eventType = jsonNode.get("event_type").asText();
                            System.out.println(eventType);
                            huorongSysLog.setTypeName(eventType);
                        }
                        huorongSysLog.setAddTime(date);
                        huorongSysLog.setLine(currentLine);
                        sysLogMapper.insertSelective(huorongSysLog);
                    } catch (Exception e) {
//                    e.printStackTrace();
                    } finally {
//                        lineCount++;
                        continue;
                    }

                }
                currentLine++;


//                while (matcher.find()) {
//                    String match = matcher.group(1);
//                    System.out.println(match);
//
//
//                }


            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
